﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MarkupSanitizer;
using viBook.Common;
using viBook.Filters;
using viBook.Helpers;
using viBook.Models;
using viBook.Repositories;

namespace viBook.Controllers
{
    public class BookCommentsController : BaseController
    {
        #region Constructors and Repository vars

        private readonly IActivityRepository _activityRepository;
        private readonly IBookCommentRepository _bookCommentRepository;

        public BookCommentsController(IBookCommentRepository bookCommentRepository,
                                      IActivityRepository activityRepository)
        {
            _bookCommentRepository = bookCommentRepository;
            _activityRepository = activityRepository;
        }

        #endregion

        [HttpPost]
        [JsonAuthentication]
        public JsonResult Create(BookComment bookcomment)
        {
            bookcomment.Content = Sanitizer.RemoveAllTags(bookcomment.Content);
            if (ModelState.IsValid)
            {
                bookcomment.UserId = ExtendedIdentity.Id;
                _bookCommentRepository.InsertOrUpdate(bookcomment);
                _bookCommentRepository.Save();
                _activityRepository.CreateActivity(bookcomment, Constant.CommentedBookType);
                _activityRepository.Save();
                return Json(new
                                {
                                    isSuccess = true,
                                    id = bookcomment.Id,
                                    username = ExtendedIdentity.Name,
                                    userImage = ExtendedIdentity.UserImage ?? Constant.DefaultUserAvatar
                                });
            }

            return FailedFeedback;
        }
        //TODO:delete comment
        [HttpPost, ActionName("Delete")]
        public JsonResult DeleteConfirmed(int id)
        {
            _bookCommentRepository.Delete(id);
            _bookCommentRepository.Save();
            _activityRepository.DeleteActivity(id, Constant.CommentedBookType);
            _activityRepository.Save();
            return SuccessFeedback;
        }

        public JsonResult GetCommentByPage(int bookId, int pageNumber)
        {
            List<BookComment> bookComments = _bookCommentRepository.FetchCommentsByPage(bookId, pageNumber, ExtendedIdentity.Id);
            return Json(new
                       {
                           isSuccess = true,
                           comments = bookComments.Select(c => new
                                                               {
                                                                   id = c.Id,
                                                                   username = c.User.DisplayName,
                                                                   userImage = c.User.AvatarUrl,
                                                                   content = c.Content,
                                                                   createdDate = c.CreatedDate.ConvertDateToString(),
                                                                   like = c.Like,
                                                                   isLiked = c.IsLiked,
                                                                   isReported = c.IsReported,
                                                                   isAuthorized = c.IsAuthorized
                                                               })
                       });
        }



        [HttpPost]
        [JsonAuthentication]
        public ActionResult Like(int id)
        {
            DataFeedback feedback = _bookCommentRepository.Like(id, ExtendedIdentity.Id);
            if (feedback.IsSuccess)
            {
                BookCommentLike bookCommentLike = _activityRepository.FindBookCommentLike(id, ExtendedIdentity.Id);
                _activityRepository.CreateActivity(bookCommentLike, Constant.LikedBookCommentType);
                _activityRepository.Save();
                return SuccessFeedback;
            }

            return Json(new { success = false, message = feedback.Message });
        }

        [HttpPost]
        [JsonAuthentication]
        public JsonResult ReportComment(int id)
        {
            _bookCommentRepository.Report(id, ExtendedIdentity.Id);
            return SuccessFeedback;
        }
    }
}